package com.yumi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yumi.entity.Announcement;
import com.yumi.mapper.AnnouncementMapper;
import com.yumi.service.EmployeeAnnouncementsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class employeeAnnouncementsServiceImpl
        extends ServiceImpl<AnnouncementMapper,Announcement>
        implements EmployeeAnnouncementsService{

    @Override
    public Page<Announcement> pageQuery(int page, int pageSize, String title, Integer type, Boolean active) {
        Page<Announcement> pageInfo = new Page<>(page, pageSize);
        
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加标题搜索条件
        if (StringUtils.hasText(title)) {
            queryWrapper.like(Announcement::getTitle, title);
        }
        
        // 添加类型筛选条件
        if (type != null) {
            queryWrapper.eq(Announcement::getType, type);
        }
        
        // 根据是否有效筛选
        if (active != null && active) {
            LocalDateTime now = LocalDateTime.now();
            queryWrapper.le(Announcement::getStartTime, now)
                    .and(wrapper -> wrapper.isNull(Announcement::getEndTime)
                            .or(wrapper2 -> wrapper2.ge(Announcement::getEndTime, now)));
        }
        
        // 按置顶状态和创建时间排序
        queryWrapper.orderByDesc(Announcement::getIsTop, Announcement::getCreateTime);
        
        return page(pageInfo, queryWrapper);
    }

    @Override
    public Announcement getAnnouncementById(Long id) {
        return getById(id);
    }

    @Override
    @Transactional
    public void updateAnnouncement(Announcement announcement) {
        updateById(announcement);
    }

    @Override
    @Transactional
    public void deleteAnnouncement(Long id) {
        removeById(id);
    }

    @Override
    @Transactional
    public void topAnnouncement(Long id) {
        Announcement announcement = getById(id);
        if (announcement != null) {
            announcement.setIsTop(1); // 设置为置顶状态
            updateById(announcement);
        }
    }

    @Override
    @Transactional
    public void untopAnnouncement(Long id) {
        Announcement announcement = getById(id);
        if (announcement != null) {
            announcement.setIsTop(0); // 设置为非置顶状态
            updateById(announcement);
        }
    }

    @Override
    public List<Announcement> getActiveAnnouncements() {
        LocalDateTime now = LocalDateTime.now();
        
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(Announcement::getStartTime, now)
                .and(wrapper -> wrapper.isNull(Announcement::getEndTime)
                        .or(wrapper2 -> wrapper2.ge(Announcement::getEndTime, now)))
                .orderByDesc(Announcement::getIsTop, Announcement::getCreateTime);
        
        return list(queryWrapper);
    }
}
